import { $ } from "../modules/public.js";
import ajax from "../modules/ajax.js";
import toast from "../modules/toast.js";

ajax({
  url:"http://localhost:3000/getuser",
  data:{
    token:sessionStorage.getItem("token") || ""
  }
}).then(res=>{
  res = JSON.parse(res);
  console.log(res)
  if(res.code === 0){
    toast({
      type:"error",
      title:"登录失效，或，当前未登录，请重新登录",
      close:()=>{
        location.assign("./login.html");
      }
    })
  }else{
    const userMsg = res.data;

    $(".avatar img").src = userMsg.avatar || "";
    $(".nickName").value = userMsg.nickName || "";
    $(".tel").innerText = userMsg.tel || "";
    $(".email").value = userMsg.email || "";
    
    // $(".birth").value = userMsg.birth || "";
    
    $(".power").innerText = userMsg.power===0 ? "用户" : "管理员";
    const registerD = new Date(userMsg.registerTime);
    $(".registerTime").innerText = registerD.toLocaleString();
    
    let loginTimeStr = "";
    if(userMsg.loginTime){
      const loginD = new Date(userMsg.loginTime);
      loginTimeStr = loginD.toLocaleString();
    }
    $(".loginTime").innerText = loginTimeStr;
  }
})

$(".openSetPw").onclick = function(){
  $(".setPasswordBox").style.display = "flex";
}
$(".setPasswordBox .close").onclick = function(){
  $(".setPasswordBox").style.display = "none";
}

$(".setPW").onclick = function(){
  // ajax()
}